package sw.EngAdroid;

import java.util.*;

/* 
 * The Computer Player class extends the Player class.
 * It overrides the submitGuess and placeBet methods.
 * 
 * submitGuess method: Jim Choinoski
 * placeBet method: Jim Jensen
 */

public class ComputerPlayer extends Player {

	Random generator = new Random();

	public ComputerPlayer(String name) {
		super(name);
	}

	// Jim Choinoski
	@Override
	public void submitGuess(int answer) {

		Random intGenerator = new Random();
		Random boolGenerator = new Random();
		int number = 0;
		boolean bool = boolGenerator.nextBoolean();
		int deviation = 0;

		// assign the range of deviation based upon the size of the answer
		if (answer < 10) {
			deviation = 5;
		} else if (answer < 100) {
			deviation = 50;
		} else {
			deviation = 100;
			// deviation = answer / 2;
		}

		// if the random boolean is true, add a random number within the
		// deviation
		// if the random boolean is false, subtract a random number within the
		// deviation
		if (bool == true) {
			number = answer + (intGenerator.nextInt(deviation) + 1);
		} else if (bool == false) {
			number = answer - (intGenerator.nextInt(deviation) + 1);
			if (number < 0) {
				number = -number;
			}
		}
				

		// System.out.println(number);
		Guess guess = new Guess(getPlayer_name(), number);
		getGame().receiveGuess(guess);
	}

	/**
	 * AI for the automated player to chose where to bet
	 */
	public void chooseBet(int num_players) {
		int choice = 0;
		int chips;
		
		// Original code causing divide by zero error if no real player bets 
		// (i.e. gameBets.size() == 0, happened when bet timer expired with no bet placed).
		// Also, all computer players were making the same bet.  Changed to randomly choose
		// amongst the available guesses.  Left code in case needed at later point.
		
		
	//	ArrayList<Bet> gameBets = currentGame.getBetList();

	//	// sum the choices
	//	for (int i = 0; i < gameBets.size(); i++) {
	//		choice += gameBets.get(i).getChoice();
	//	}

	//	choice = Math.round(choice / gameBets.size()); // pick the consensus
														// choice

		choice = generator.nextInt(num_players);
		
		chips = getPermanent_chips() + getGame_chips(); // go all in

		placeBet(choice, chips);
	}

}
